package uns.fit.gis.ui;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.NokiaKeyDef;
import uns.fit.gis.mapdrawer.ActionCode;

public class MenuCanvas extends Canvas implements CommandListener {
	private Command backCommand;
	private Command selectCommand;
	private MapDrawer midlet;
	
	private static final Font lowFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
	private static final Font highFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
	
	private static final int lowColor = 0x000000FF;
	private static final int highColor = 0x00FF0000;
	private static final int highBGColor = 0x00CCCCCC;
	
	private static int width;
	private static int height;
	
	private static int startHeight;
	
	private static int spacing = highFont.getHeight() / 2;
	
	private static final String[] mainMenu = {
						"GPS Press 0",
						"Search path Press 1",
						"Search location Press 2",
						"Set source point Press 3",
						"Set destination point Press 4",
						"View best route result Press 5",
						"Clear best route result Press 6",
						"Camera Press 7",
						"Recorder Press 8",	
                        };
	
	private static int menuId;

	public MenuCanvas(MapDrawer midlet) {
		width = getWidth();
		height = getHeight();
		startHeight = (highFont.getHeight() * mainMenu.length) +
						((mainMenu.length - 1) * spacing);
		startHeight = (height - startHeight) / 2;
		
		menuId = 0;
		
		this.midlet = midlet;
		backCommand = new Command("Back", Command.BACK, 1);
		addCommand(backCommand);
		selectCommand = new Command("Select", Command.OK, 1);
		addCommand(selectCommand);
		setCommandListener(this);
	}

	protected void paint(Graphics g) {
		g.setColor(0x00FFFFFF);
		g.fillRect(0, 0, width, height);
		
		for(int i = 0; i < mainMenu.length; i++)
		{
			if(i == menuId)
			{
				g.setColor(highBGColor);
				g.fillRect(0, startHeight + (i * highFont.getHeight()) +
							spacing, width, highFont.getHeight());
				g.setFont(highFont);
				g.setColor(highColor);
				g.drawString(mainMenu[i],
								(width - highFont.stringWidth(mainMenu[i])) / 2,
								startHeight + (i * highFont.getHeight()) + spacing,
								20);
			}
			else
			{
				g.setFont(lowFont);
				g.setColor(lowColor);
				g.drawString(mainMenu[i],
								(width - lowFont.stringWidth(mainMenu[i])) / 2,
								startHeight + (i * highFont.getHeight()) + spacing,
								20);
			}
		}
	}

	public void commandAction(Command command, Displayable displayable) {
		if(command == backCommand)
			midlet.showMapCanvas();
		else if(command == selectCommand) {
			switch(menuId) {
			case 0:
				keyPressed(KEY_NUM0);
				break;
			case 1:
				keyPressed(KEY_NUM1);
				break;
			case 2:
				keyPressed(KEY_NUM2);
				break;
			case 3:
				keyPressed(KEY_NUM3);
				break;
			case 4:
				keyPressed(KEY_NUM4);
				break;
			case 5:
				keyPressed(KEY_NUM5);
				break;
			case 6:
				keyPressed(KEY_NUM6);
				break;
			case 7:
				keyPressed(KEY_NUM7);
				break;
			case 8:
				keyPressed(KEY_NUM8);
				break;
			default:
				break;
			}
		}
	}

	protected void keyPressed(int keyCode) {
		switch(keyCode) {
		case KEY_NUM0:
			//TODO: do what when press key 0?
			break;
		case KEY_NUM1:
			midlet.mapCanvas.commandAction(ActionCode.SHOW_MAP);
			break;
		case KEY_NUM2:
			midlet.mapCanvas.commandAction(ActionCode.RUN_GPS);
			break;
		case KEY_NUM3:
			midlet.mapCanvas.commandAction(ActionCode.SEARCH_PATH);
			break;
		case KEY_NUM4:
			midlet.mapCanvas.commandAction(ActionCode.SEARCH_LOCATION);
			break;
		case KEY_NUM5:
			midlet.mapCanvas.commandAction(ActionCode.SHOW_BEST_ROUTE);
			break;
		case KEY_NUM6:
			midlet.mapCanvas.commandAction(ActionCode.TAKE_PICTURE);
			break;
		case KEY_NUM7:
			midlet.mapCanvas.commandAction(ActionCode.ASSIST_DRIVING);
			break;
		case KEY_NUM8:
			midlet.mapCanvas.commandAction(ActionCode.SHOW_SETTINGS_FORM);
			break;
		case KEY_NUM9:
			midlet.mapCanvas.commandAction(ActionCode.DESTROY_APP);
			break;
		
		case NokiaKeyDef.NOKIA_KEY_RIGHT_ARROW:
			
			break;
		case NokiaKeyDef.NOKIA_KEY_LEFT_ARROW:
			
			break;
		case NokiaKeyDef.NOKIA_KEY_UP_ARROW:
			if(menuId > 0)
				menuId --;

			else
				menuId = mainMenu.length - 1;
			repaint();
			break;
		case NokiaKeyDef.NOKIA_KEY_DOWN_ARROW:
			if(menuId < mainMenu.length - 1)
				menuId ++;
			else
				menuId = 0;
			repaint();
			break;
		default:
			break;
		}
	}
}
